package com.longnuoqi.mydoctor.activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.longnuoqi.mydoctor.entity.Favorite;
import com.longnuoqi.mydoctor.fragment.UserFavoriteFragment;
import com.longnuoqi.mydoctor.fragment.UserFavoriteNoneFragment;
import com.longnuoqi.mydoctor.utils.ActivityCollection;
import com.longnuoqi.mydoctor.utils.FileUtil;
import com.yunhe.mydoctor.R;

import java.util.ArrayList;
import java.util.Timer;
import java.util.TimerTask;

/**
 * Created by Lenovo on 2016/1/9.
 */
public class UserFavoriteActivity extends AppCompatActivity {
    private SharedPreferences sharedPreferences;
    private String access_token;
    private String url;
    private ImageView back;
    private static boolean isExit = false;//判断是否退出
    private static boolean hasTask = false;//判断是否是任务
    private Timer timer = new Timer();//声明timer对象，计时器
    private TimerTask timerTask = new TimerTask() {//为timer安排一次执行或重复执行的任务
        @Override
        public void run() {
            isExit = false;
        }
    };

    /*双击退出程序*/
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
            if (!isExit) {
                isExit = true;
                Toast.makeText(UserFavoriteActivity.this, "再按一次退出程序", Toast.LENGTH_SHORT).show();
                if (!hasTask) {
                    timer.schedule(timerTask, 2000);//安排在指定延迟后执行指定的任务
                }
            } else {
                ActivityCollection.finishAll();
                //回收资源
                System.exit(0);
            }
            return false;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    protected void onTitleChanged(CharSequence title, int color) {
        super.onTitleChanged(title, color);
        setContentView(R.layout.user_favorite_layout);
        ActivityCollection.addActivity(this);
        init();
        initListView();

    }


    private void init() {
        sharedPreferences = getSharedPreferences("userlogin", Context.MODE_PRIVATE);
        access_token = sharedPreferences.getString("access_token", "");
        url = "http://www.tngou.net/api/my/favorite?access_token=" + access_token;
        back = (ImageView) findViewById(R.id.user_favour_back_btn);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = getIntent();
                setResult(RESULT_OK, intent);
                finish();
            }
        });
    }

    private void initListView() {
        RequestQueue queue = Volley.newRequestQueue(UserFavoriteActivity.this);
        StringRequest request = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
            @Override
            public void onResponse(String s) {
                ArrayList<Favorite> list = FileUtil.jsonUserFavorite(s);
                FragmentManager fm = getSupportFragmentManager();
                FragmentTransaction transaction = fm.beginTransaction();
                Fragment fragment = null;
                if (list.size() == 0) {
                    fragment = new UserFavoriteNoneFragment();
                } else {
                    fragment = new UserFavoriteFragment();
                    Bundle bundle = new Bundle();
                    bundle.putParcelableArrayList("favorite", list);
                    fragment.setArguments(bundle);
                }
                transaction.replace(R.id.user_favorite_framelayout, fragment);
                transaction.commit();

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {

            }
        });
        queue.add(request);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ActivityCollection.removeActivity(this);
    }
}
